package com.casework.auth

import grails.converters.JSON
import grails.plugin.spock.ControllerSpec
import grails.plugins.springsecurity.SpringSecurityService

class CompanyControllerSpec extends ControllerSpec {

    def setup(){
        registerMetaClass(AuthUserRole)
        AuthUserRole.metaClass.static.removeAll = { AuthUser a ->}
    }

    def "delete user if not yourself"() {
        given:
        def user = new AuthUser(id: 123)
        mockDomain AuthUser, [user]
        mockDomain Company, [new Company(id: 1)]
        mockDomain AuthUserRole, [new AuthUserRole(authUser: user)]
        def security = Mock(SpringSecurityService)
        controller.springSecurityService = security
        security.currentUser >> new AuthUser(id:9999)

        when:
        controller.params.id = 123
        controller.params.companyId = 1
        controller.deleteUser()

        then:
        JSON.parse(mockResponse.contentAsString).result == 'success'
    }

    def "Cannot delete user if that user is yourself"(){
        given:
        def user = new AuthUser(id: 123)
        mockDomain AuthUser, [user]
        mockDomain Company, [new Company(id: 1)]
        mockDomain AuthUserRole, [new AuthUserRole(authUser: user)]
        def security = Mock(SpringSecurityService)
        controller.springSecurityService = security
        security.currentUser >> user

        when:

        controller.params.id = 123
        controller.params.companyId = 1
        controller.deleteUser()

        then:
        JSON.parse(mockResponse.contentAsString).result == 'failure'
    }

}
